﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TemplateWizard;
using System.Windows.Forms;
using System.IO;
using EnvDTE;

namespace WizardExtension
{
    public class DAInterfaceExtension : ExtensionBase, IWizard
    {
        public void BeforeOpeningFile(EnvDTE.ProjectItem projectItem)
        {

        }

        public void ProjectFinishedGenerating(EnvDTE.Project project)
        {

        }

        public void ProjectItemFinishedGenerating(EnvDTE.ProjectItem projectItem)
        {
            string interfaceName = projectItem.Name;
            if (interfaceName.Contains("."))
                interfaceName = interfaceName.Substring(0, interfaceName.IndexOf("."));

            bool found = false;

            foreach (ProjectItem pi in projectItem.ContainingProject.ProjectItems)
            {
                for (short i = 0; i < pi.FileCount; i++)
                {
                    string filePath = pi.FileNames[i];

                    if (filePath.EndsWith("AccessPoint\\"))
                    {
                        found = true;

                        AddDAInterfacePropertyCode(filePath + "IDataAccessAccessPoint.cs", interfaceName);
                    }
                }
            }

            if (!found)
                MessageBox.Show("Unable to find Access Point Interface!" + Environment.NewLine + "Please add the new Type properties manually.");
        }

        private void AddDAInterfacePropertyCode(string filePath, string interfaceName)
        {
            string className = interfaceName;
            if (className.StartsWith("I"))
                className = className.Substring(1, className.Length - 1);

            string fileContent = File.ReadAllText(filePath);
            string propertyContent = "     " + interfaceName + " " + className + " { get; }";

            int bracesCount = fileContent.Count(ch => ch == '}');
            int c = 0;
            for (int i = 0; i < fileContent.Length; i++)
                if (fileContent[i] == '}')
                {
                    c++;

                    if (c == (bracesCount - 1))
                    {
                        fileContent = fileContent.Substring(0, i - 1) + propertyContent + Environment.NewLine + "    }" + Environment.NewLine + "}";
                    }
                }

            File.WriteAllText(filePath, fileContent);
        }

        public void RunFinished()
        {

        }

        public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            AddBaseNamespace(automationObject, replacementsDictionary);

            string interfaceName = replacementsDictionary["$rootname$"];
            string entityName = interfaceName.Replace(".cs", string.Empty);

            if (entityName.StartsWith("I"))
                entityName = entityName.Substring(1, entityName.Length - 1);

            if (entityName.EndsWith("DA"))
                entityName = entityName.Substring(0, entityName.Length - 2);

            replacementsDictionary.Add("$safeentityname$", entityName);
        }

        public bool ShouldAddProjectItem(string filePath)
        {
            return true;
        }
    }
}

